const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {   
        time:0,
        showAdver:true,
        interval:"",
        showAdTime:false,
        timers:0
       },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad:function(options) {
        var that=this;
        console.log('----login----options----')
        console.log(options);
        console.log('-----login---options----');
      
        var enterOptions =wx.getEnterOptionsSync();
        console.log(enterOptions);
        var path = enterOptions.path;
        if(path == "pages/login/index"){
            path="pages/index/index";
        }
        this.setData({
            path:path
        });
        console.log(path);
        var query=enterOptions.query;
        if(Object.keys(query).length === 0){
            console.log('----share index----query----');
            
        }else{
            var parent_id = 0;
            console.log(query)
            if(query.hasOwnProperty('parent_id')){
                console.log('---has parent_id---');
               var parent_id = prquery.parent_id;
               this.setData({
                parent_id:query.parent_id
              })
              wx.setStorageSync('parent_id',query.parent_id);
            }   
        }
    //   this.getLoginImage();
 
      
       that.wxLogin(); //that.wxLogIn();  
     
    },
    wxLogin:function(){
      //获取个人资料缓存
      var open = wx.getStorageSync('open');
       console.log(open);
       //var date=util.formatTime(new Date());
       if(open){
        var id=open.id;
        var parent_id = wx.getStorageSync('parent_id');
        if(parent_id==undefined){
            parent_id=0;
          }
          if(parent_id==''){
            parent_id=0;
          }

       // id=undefined;
        let that=this;
        wx.request({
          url: app.globalData.domain+'/wechat/login/getUser',
          data:{id:id,from:3,parent_id:parent_id},
          success:res=>{        
            console.log(res);
            if(res.statusCode==200){
                console.log(res.statusCode);         
              wx.setStorageSync('open',res.data.open); 
              that.showAdvert(); 
            }else{
                console.log(res.statusCode);
               that.getUnionId();
            }                             
          }
        })   
       }else{
           this.getUnionId(); 
        }
    },
    getUnionId:function(){
      var that=this;
      var parent_id=wx.getStorageSync('parent_id');
     wx.login({
        success:res=>{
          console.log(res)
          var code =res.code;  
              wx.request({
                url: app.globalData.domain+'/wechat/login/getOpen',
                data:{code:code,parent_id:parent_id},
                success:function(res){    
                    console.log(res);     
                 let data = res.data;
                  if(res.statusCode==200){   
      
                wx.setStorageSync('open', data.open)  
                //wx.setStorageSync('user',data.user);                            
                  that.showAdvert();              
                 
                }
                }
              })
            }
          })
        
      
    },
    getLoginImage:function(){
        let that = this;
        wx.request({
          url: app.globalData.domain+'login/getLoginImage',
          data:{from:1},
          success:res=>{
            that.setData({
              loginImage:res.data.image
            })
          }
      })
      },
      showAdvert:function(){
        var parent_id=wx.getStorageSync('parent_id');

        var path = this.data.path;
        console.log(path);
        wx.reLaunch({
            url: '/'+path+'?parent_id='+parent_id,
          });
        this.setData({
          showAdTime:true
        });
        let that = this;
        var timers;
        //that.data.timers = setInterval(that.toTimer,1000);   
  
       　
    },
    toTimer:function(){
        var time=this.data.time;
        let that=this;
       // console.log(this.data.timers)
        if(time>=0){
            clearInterval(that.data.timers);
          //  console.log(time);        
            this.setData({
               time:0
            });
            wx.reLaunch({
                url: '../index/index',
              })
        }else if(time > 3){
            clearInterval(that.data.timers);
        }else {  
            time = time + 1;         
            //console.log(time);
            this.setData({
                time:time
            }) 
        }
    },
         
   cliadv: function() {
       　　clearInterval(this.data.timers)
       　   this.setData({
               showAdver:false,
               time:9
             })
             wx.reLaunch({
             url: '../index/index',
           })　
       },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        clearInterval(this.data.timers);
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        clearInterval(this.data.timers);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})